<?php

namespace Home\Controller;

class TaskProductLogController extends HomeController {
	
	
     /**
     * 首页(non-PHPdoc)
     * @see \Home\Controller\HomeController::index()
     */
    function index() {
    	
        $plugin['date'] = true;
        $this -> assign("plugin", $plugin);
    	$d = $_POST;
    	$model = M("task_product_log");
    	$map = array();
    	$user_array = array();
    	if (!empty($d['be_input_date']) && !empty($d['en_input_date'])) {
            $map['create_at'] = array(array('EGT',$d['be_input_date'].' 00:00:00'),array('ELT',$d['en_input_date'].' 23:59:59'));
    	}
    	if (!empty($d['be_input_date']) && empty($d['en_input_date'])) {
    	    $map['create_at'] = array('EGT',$d['be_input_date'].' 00:00:00');
    	}
    	if (empty($d['be_input_date']) && !empty($d['en_input_date'])) {
    	    $map['create_at'] = array('ELT',$d['en_input_date'].' 23:59:59');
    	}
    	if (!empty($d['eq_dept_id'])) {
    	    $map['dept_id'] = $d['eq_dept_id'];
    	}
    	$user_array = M("User") -> field('id,name')->select();
    	if (!empty($d['user_id'])) {
    	    $map['user_id'] =  $d['user_id'];
    	}
    
    	$node = D("Dept");
    	$dept_id = get_dept_id();
    	$dept_name = get_dept_name();
    		
    	$menu = array();
    	$dept_menu = $node -> field('id,pid,name') -> where("is_del=0") -> order('sort asc') -> select();
    	$dept_tree = list_to_tree($dept_menu);
    	$count = count($dept_tree);
    	if (empty($count)) {
    	    /*获取部门列表*/
    	    $html = '';
    	    $html = $html . "<option value='{$dept_id}'>{$dept_name}</option>";
    	    $this -> assign('dept_list', $html);
    	} else {
    	    /*获取部门列表*/
    	    $this -> assign('dept_list', select_tree_menu($dept_tree));
    	}
    	
    	$taskProductLog = $this -> _list($model, $map);
    	$task = $this->getTask($taskProductLog);
    	$this->assign('task',$task);
    	$this->assign('be_input_date',I('post.be_input_date',''));
    	$this->assign('en_input_date',I('post.en_input_date',''));
    	$this->assign('user_id',I('post.user_id',''));
    	$this->assign('user_array',$user_array);
    	$this->display();
    }
    
     /**
     * 获取任务名称
     * @param unknown $taskProductLog
     */
    private function getTask($taskProductLog) {

        $taskId = array();
        $taskName = array();
        foreach ($taskProductLog as $val) {
            $taskId[] = $val['task_id'];
        }
        $map['id'] = array('in',implode(',',$taskId));
        $task = M('task');
        $TaskResult = $task->where($map)->field('id,name')->select();
        foreach ($TaskResult as $item) {
            $taskName[$item['id']] = $item['name'];
        }
        return $taskName;
    }
    
    /**
     * 个人绩效查询
     */
    public function personalProfit() {
					
    	$plugin['date'] = true;
    	$this -> assign("plugin", $plugin);
    	$d = $_POST;
    	$model = M("task_product_log");
    	$map = array();
    	if (!empty($d['be_input_date']) && !empty($d['en_input_date'])) {
    		$map['create_at'] = array(array('EGT',$d['be_input_date'].' 00:00:00'),array('ELT',$d['en_input_date'].' 23:59:59'));
    	}
    	if (!empty($d['be_input_date']) && empty($d['en_input_date'])) {
    		$map['create_at'] = array('EGT',$d['be_input_date'].' 00:00:00');
    	}
    	if (empty($d['be_input_date']) && !empty($d['en_input_date'])) {
    		$map['create_at'] = array('ELT',$d['en_input_date'].' 23:59:59');
    	}
    	$map['user_id'] = get_user_id();
    	$taskProductLog = $this -> _list($model, $map);
    	$task = $this->getTask($taskProductLog);
    	$this->assign('task',$task);
    	$this->assign('be_input_date',I('post.be_input_date',''));
    	$this->assign('en_input_date',I('post.en_input_date',''));
    	$this->display();
    }
}